package com.xiaomi.baidumap.hotuser;

import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.mugen.Mugen;
import com.mugen.MugenCallbacks;
import com.xiaomi.baidumap.R;
import com.xiaomi.baidumap.common.ActivityUtils;
import com.xiaomi.baidumap.components.FooterView;
import com.xiaomi.baidumap.model.User;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.header.StoreHouseHeader;

/**
 * Created by Administrator on 16-8-31.
 */
public class HotUserFragment extends Fragment implements HotUserPresenter.HotUserView{

    @BindView(R.id.lvRepos)
    ListView lvRepos;
    @BindView(R.id.ptrClassicFrameLayout)
    PtrClassicFrameLayout ptrClassicFrameLayout;
    @BindView(R.id.emptyView)
    TextView emptyView;
    @BindView(R.id.errorView)
    TextView errorView;

    private HotUserAdapter adapter;
    private ActivityUtils activityUtils;
    private HotUserPresenter presenter;
    private FooterView footerView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_hot_user,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        activityUtils = new ActivityUtils(this);
        presenter = new HotUserPresenter(this);
        adapter = new HotUserAdapter();
        lvRepos.setAdapter(adapter);

        initPullToRefresh();

        initPushToLoad();

        //如果没有数据,就自动刷新
        if (adapter.getCount() <= 0){
            ptrClassicFrameLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //第三方自带的自动刷新
                   ptrClassicFrameLayout.autoRefresh();
                }
            },200);
        }
    }

    //第三方刷新的配置
    public void initPullToRefresh(){
        ptrClassicFrameLayout.setLastUpdateTimeRelateObject(this);
        ptrClassicFrameLayout.setDurationToClose(1000);

        //完成刷新的操作
        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.refresh();
            }
        });

        // 以下代码（只是修改了header样式）
        StoreHouseHeader header = new StoreHouseHeader(getContext());
        header.initWithString("I LIKE " + " JAVA");
        header.setPadding(0, 60, 0, 60);
        // 修改Ptr的HeaderView效果
        ptrClassicFrameLayout.setHeaderView(header);
        ptrClassicFrameLayout.addPtrUIHandler(header);
        ptrClassicFrameLayout.setBackgroundResource(R.color.colorRefresh);
    }

    //加载视图的配置
    public void initPushToLoad(){
        footerView = new FooterView(getContext());
        Mugen.with(lvRepos, new MugenCallbacks() {
            @Override
            public void onLoadMore() {
                presenter.loadMore();
            }

            @Override
            public boolean isLoading() {
                return lvRepos.getFooterViewsCount() > 0 && footerView.isLoading();
            }

            @Override
            public boolean hasLoadedAllItems() {
                return lvRepos.getFooterViewsCount() > 0 && footerView.isComplete();
            }
        }).start();
    }

    //刷新和加载的视图
    @Override
    public void showRefreshView() {
        ptrClassicFrameLayout.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.GONE);
        errorView.setVisibility(View.GONE);
    }

    @Override
    public void refreshData(List<User> users) {
        //首先清除原先的数据
        adapter.clean();
        adapter.addAll(users);
    }

    @Override
    public void stopRefresh() {
        //第三方自带的停止刷新
        ptrClassicFrameLayout.refreshComplete();
    }

    @Override
    public void showErrorView() {
        ptrClassicFrameLayout.setVisibility(View.GONE);
        errorView.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.GONE);
    }

    @Override
    public void showEmptyView() {
        emptyView.setVisibility(View.VISIBLE);
        ptrClassicFrameLayout.setVisibility(View.GONE);
        errorView.setVisibility(View.GONE);
    }

    @Override
    public void showMessage(String msg) {
        activityUtils.showToast(msg);
    }

    @Override
    public void addLoadData(List<User> users) {
        adapter.addAll(users);
    }

    @Override
    public void showLoadView() {
        //首先判断
        if (lvRepos.getFooterViewsCount() == 0){
            lvRepos.addFooterView(footerView);
        }
        footerView.showLoading();
    }

    @Override
    public void hideLoadView() {
        lvRepos.removeFooterView(footerView);
    }
}
